package org.rency.crawler.fetch.service.policy.impl;

import javax.annotation.Resource;
import org.rency.crawler.fetch.helper.FetchCarrier;
import org.rency.crawler.fetch.repository.FetchedQueueRepository;
import org.rency.crawler.fetch.service.policy.PolicyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 描述: 去重校验<br>
 * 最近修改时间:2017/11/22 18:51<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/11/22 18:51
 */
@Service("duplicatePolicyService")
public class DuplicatePolicyServiceImpl implements PolicyService {

  private static final Logger logger = LoggerFactory.getLogger(DuplicatePolicyServiceImpl.class);

  @Resource
  private FetchedQueueRepository fetchedQueueRepository;

  @Override
  public String getPolicyName() {
    return "抓取去重";
  }

  @Override
  public boolean accept(FetchCarrier carrier) {
    boolean fetched = fetchedQueueRepository.exists(carrier.getRequest().getFetchNo());
    if(!fetched){
      fetchedQueueRepository.save(carrier.getRequest().getFetchNo(),carrier.getRequest().getFetchUri());
    }
    if(fetched){
      logger.debug("策略校验[{}]未通过,{}.",this.getPolicyName(),carrier.getRequest().getFetchUri());
    }
    return !fetched;
  }

  @Override
  public boolean retrySupport() {
    return false;
  }
}
